﻿<?php 
	session_start();
	require_once './model/student.php';
	require_once './model/contact.php';
	
	// Clear all previous messages.
	if($_SESSION['log-err']) session_unregister('log-err');
	if($_SESSION['log-info']) session_unregister('log-info');
	
	// Chech whether cancel button is clicked.
	if(isset($_POST['cancelButton'])){
		$_SESSION['page'] = "home.php";
		header('Location: ../');
	}else{
		
		// Loading submitted parameters.
		// Student Details.
		$studentIndex = $_POST['studentIndex'];
		$studentNameWithInitials = $_POST['studentNameWithInitials'];
		$studentFullName = $_POST['studentFullName'];
		$studentGender = $_POST['studentGender'];
		$studentDateOfBirth = $_POST['studentDateOfBirth'];
		$studentAddress = $_POST['address'];
		$studentLandPhone = $_POST['landPhone'];
		$studentMobilePhone = $_POST['mobilePhone'];
		$studentEMail = $_POST['email'];
		$studentFax	= $_POST['fax'];
		$studentHealthInfo = $_POST['studentHealthInfo'];
		$studentSpecialSkills = $_POST['studentSpecialSkills'];
		
		// Father Details.
		$fatherName = $_POST['fatherName'];
		$fatherAddress = $_POST['fatherAddress'];
		$fatherLandPhone = $_POST['fatherLandPhone'];
		$fatherMobilePhone = $_POST['fatherMobilePhone'];
		$fatherEmail = $_POST['fatherEmail'];
		$fatherFax = $_POST['fatherFax'];
		$fatherOccupation = $_POST['fatherOccupation'];
		$fatherOfficeAddress = $_POST['fatherOfficeAddress'];
		$fatherOfficePhone = $_POST['fatherOfficePhone'];
		$fatherOfficeMobilePhone = $_POST['fatherOfficeMobilePhone'];
		$fatherOfficeEmail = $_POST['fatherOfficeEmail'];
		$fatherOfficeFax = $_POST['fatherOfficeFax'];
		
		// Mother Details.
		$motherName = $_POST['motherName'];
		$motherAddress = $_POST['motherAddress'];
		$motherLandPhone = $_POST['motherLandPhone'];
		$motherMobilePhone = $_POST['motherMobilePhone'];
		$motherEmail = $_POST['motherEmail'];
		$motherFax = $_POST['motherFax'];
		$motherOccupation = $_POST['motherOccupation'];
		$motherOfficeAddress = $_POST['motherOfficeAddress'];
		$motherOfficePhone = $_POST['motherOfficePhone'];
		$motherOfficeMobilePhone = $_POST['motherOfficeMobilePhone'];
		$motherOfficeEmail = $_POST['motherOfficeEmail'];
		$motherOfficeFax = $_POST['motherOfficeFax'];
		
		// Guardian Details.
		$guardianName = $_POST['guardianName'];
		$guardianAddress = $_POST['guardianAddress'];
		$guardianLandPhone = $_POST['guardianLandPhone'];
		$guardianMobilePhone = $_POST['guardianMobilePhone'];
		$guardianEmail = $_POST['guardianEmail'];
		$guardianFax = $_POST['guardianFax'];
		$guardianOccupation = $_POST['guardianOccupation'];
		$guardianOfficeAddress = $_POST['guardianOfficeAddress'];
		$guardianOfficePhone = $_POST['guardianOfficePhone'];
		$guardianOfficeMobilePhone = $_POST['guardianOfficeMobilePhone'];
		$guardianOfficeEmail = $_POST['guardianOfficeEmail'];
		$guardianOfficeFax = $_POST['guardianOfficeFax'];
		
		
		// Check for the mandatory fields.
		if($studentNameWithInitials == "" || $studentFullName == "" || $studentDateOfBirth == "" || 
			$studentAddress == "" || $guardianName == "" || $guardianAddress == ""){
			$_SESSION['log-err'] = "අත්‍යවශ්‍ය තොරතුරු සම්පූර්ණ කර නොමැත.";
			$_SESSION['page'] = "addStudent.php";
			header('Location: ../');
		}else{
			require_once './model/dbconnection.php';
			$DB = new dbConnection();	
			
			// Check student exits and StudentIndex is already used.
			$studentExists = mysql_query("SELECT * FROM students WHERE studentFullName = '$studentFullName'");
			$indexExists = mysql_query("SELECT * FROM students WHERE studentIndex = '$studentIndex'");
			if(mysql_num_rows($studentExists) > 0){
				$_SESSION['log-err'] = "සිසුවා දැනටමත් පද්ධතිය තුළ ලියාපදිංචි කර ඇත.";
				$_SESSION['page'] = "addStudent.php";
				header('Location: ../');
			}else if(mysql_num_rows($indexExists) > 0){
				$_SESSION['log-err'] = $studentIndex." ඇතුල්වීමේ අංකයෙන් සිසුවකු ලියාපදිංචි කර ඇත.";
				$_SESSION['page'] = "addStudent.php";
				header('Location: ../');
			}else{
				mysql_query("BEGIN");
				$result;
				try{
					// Adding a student.
					$student = student::fillStudent($studentIndex, $studentNameWithInitials, $studentFullName, $studentGender, $studentDateOfBirth, $studentHealthInfo, $studentSpecialSkills, $guardianName, $guardianOccupation, $fatherName, $fatherOccupation, $motherName, $motherOccupation);
					$studentId = $student->addStudent();
					$result['addStudent'] = ($studentId > 0)?true:false;
					
					// Adding student contact.
					$studentContact = contact::fillContact($studentId, 'S', $studentAddress, $studentLandPhone, $studentMobilePhone, $studentEMail, $studentFax);
					$studentContactId = $studentContact->addContact();
					$result['studentContact'] = ($studentContactId > 0)?true:false;
					
					// Guardian contact query.
					if($guardianName != "" && $guardianAddress != ""){
						$guardianContact = contact::fillContact($studentId, 'SG', $guardianAddress, $guardianLandPhone, $guardianMobilePhone, $guardianEMail, $guardianFax);
						$guardianContactId = $guardianContact->addContact();
						$result['guardianContact'] = ($guardianContactId > 0)?true:false;
					}						
					
					// Guardian office contact query.
					if($guardianName != "" && $guardianOccupation != "" && $guardianOfficeAddress != ""){
						$guardianOfficeContact = contact::fillContact($studentId, 'SGO', $guardianOfficeAddress, $guardianOfficeLandPhone, $guardianOfficeMobilePhone, $guardianOfficeEMail, $guardianOfficeFax);
						$guardianOccupationContactId = $guardianOfficeContact->addContact();
						$result['guardianOfficeContact'] = ($guardianOccupationContactId > 0)?true:false;
					}
										
					// Father contact query.
					if($fatherName !=  "" && $fatherAddress != ""){
						$fatherContact = contact::fillContact($studentId, 'SF', $fatherAddress, $fatherLandPhone, $fatherMobilePhone, $fatherEMail, $fatherFax);
						$fatherContactId = $fatherContact->addContact();
						$result['fatherContact'] = ($fatherContactId > 0)?true:false;
					}
						
									
					// Father office contact query.
					if($fatherName !=  "" && $fatherOccupation != "" && $fatherOfficeAddress != ""){
						$fatherOfficeContact = contact::fillContact($studentId, 'SFO', $fatherOfficeAddress, $fatherOfficeLandPhone, $fatherOfficeMobilePhone, $fatherOfficeEMail, $fatherOfficeFax);
						$fatherOccupationContactId = $fatherOfficeContact->addContact();
						$result['fatherOfficeContact'] = ($fatherOccupationContactId > 0)?true:false;
					}	
					
					// Mother contact query.
					if($motherName != "" && $motherAddress != ""){
						$motherContact = contact::fillContact($studentId, 'SM', $motherAddress, $motherLandPhone, $motherMobilePhone, $motherEMail, $motherFax);
						$motherContactId = $motherContact->addContact();
						$result['motherContact'] = ($motherContactId > 0)?true:false;
					}
					
					// Mother office contact query.
					if($motherName != "" && $motherOccupation != "" && $motherOfficeAddress != ""){
						$motherOfficeContact = contact::fillContact($studentId, 'SMO', '$motherOfficeAddress','$motherOfficeLandPhone','$motherOfficeMobilePhone','$motherOfficeEMail','$motherOfficeFax');
						$motherOccupationContactId = $motherOfficeContact->addContact();
						$result['motherOfficeContact'] = ($motherContactId > 0)?true:false;
					}
					
					if(!in_array(false, $result)){
						// Commit queries.
						mysql_query("COMMIT");
						$_SESSION['log-info'] = "සිසුවා සාර්ථකව ලියාපදිංචි කරන ලදි!";
						$_SESSION['page'] = "addStudent.php";
						header('Location: ../');
					}else{
						// Rollback queries.
						mysql_query("ROLLBACK");
						$_SESSION['log-err'] = "සමාවන්න! ලියාපදිංචිය අසාර්ථක උනි.";
						$_SESSION['page'] = "addStudent.php";
						header('Location: ../');
					}				
				} catch(Exception $e){
					// Rollback queries.
					mysql_query("ROLLBACK");
					$_SESSION['log-err'] = "සමාවන්න! ලියාපදිංචිය අසාර්ථක උනි.";
					$_SESSION['page'] = "addStudent.php";
					header('Location: ../');
				}
			}  
		}
	}
?>